package cn.ainit.base.product;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.base.product.entity.ProductInfo;
import cn.ainit.common.ReturnObject;
import cn.ainit.sys.menu.SessionCont;
import cn.ainit.sys.utils.UUID32;

@Controller
@RequestMapping("/productInfo")
public class ProductController {
	private ProductService productService;
	
	@Resource
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	 
	@RequestMapping("saOrUpdProduct")
	@ResponseBody
	public ReturnObject saveProduct(ProductInfo productInfo,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			boolean flag = false;
			String hid = "";  //productService.findProduct(productInfo.getHid())
			if (productInfo.getHid() == null || productInfo.getHid()=="" || productInfo.getHid().equals("null") || productInfo.getHid().equals("")) {
				hid =UUID32.newGuid();
				productInfo.setHid(hid);
				flag = productService.saveProduct(productInfo);
				if (flag) {
					returnObject.setMsg("success");
					returnObject.setInfo(hid);
				}else{
					returnObject.setMsg("fail");
				}
			}else{
				flag = productService.updProduct(productInfo);
				if (flag) {
					returnObject.setMsg("upsuccess");
				}else {
					returnObject.setMsg("fail");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return returnObject ;
	}
	/**
	 * 根据选题的书号去查找相应的商品
	 * @param isbns
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-6-10 下午03:19:35      
	 */
	@RequestMapping("/isbnFindPro")
	@ResponseBody
	public ReturnObject xtFindPro(String isbns,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			List prods = this.productService.xtFindProd(isbns);
			if(prods == null){
				prods = new ArrayList();
			}
			returnObject.setObjList(prods);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return returnObject ;
	}
	/**
	 * 根据商品执行函数查询商品在各个库房的库存数
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/stockNumByPro")
	@ResponseBody
	public List<String> stockNumByPro(String proId,HttpServletRequest request,HttpSession session) throws Exception{
		List<String> list = new ArrayList<String>();
		String stockNumStr = "";
		try {
			Object uid = session.getAttribute(SessionCont.uid);
			if (uid == null || "".equals(uid.toString())) {
				System.out.println("用户未登录 FrameworkController L.318");
			}
			stockNumStr = this.productService.execProFunc(proId, "", uid.toString());
			list.add(stockNumStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
